package com.mall.product.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.mall.common.core.domain.R;
import com.mall.product.domain.res.PmsProductCategoryRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;
import com.mall.common.log.annotation.Log;
import com.mall.common.log.enums.BusinessType;
import com.mall.common.security.annotation.RequiresPermissions;
import com.mall.product.domain.PmsProductCategory;
import com.mall.product.service.ICategoryService;
import com.mall.common.core.web.controller.BaseController;
import com.mall.common.core.web.domain.AjaxResult;
import com.mall.common.core.utils.poi.ExcelUtil;
import com.mall.common.core.web.page.TableDataInfo;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/categoryInfo")
public class PmsProductCategoryController extends BaseController
{
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("product:categoryInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(PmsProductCategory category)
    {
        startPage();
        List<PmsProductCategory> list = categoryService.selectCategoryList(category);
        return getDataTable(list);
    }
    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("product:categoryInfo:categoryList")
    @GetMapping("/categoryList")
    public TableDataInfo categoryList()
    {
        startPage();
        List<PmsProductCategoryRes> list = categoryService.queryProductLevel1ClassificationInformation();
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("product:categoryInfo:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsProductCategory category)
    {
        List<PmsProductCategory> list = categoryService.selectCategoryList(category);
        ExcelUtil<PmsProductCategory> util = new ExcelUtil<PmsProductCategory>(PmsProductCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @RequiresPermissions("product:categoryInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(categoryService.selectCategoryById(id));
    }

    /**
     * 新增商品分类
     */
    @RequiresPermissions("product:categoryInfo:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsProductCategory category)
    {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("product:categoryInfo:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsProductCategory category)
    {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("product:categoryInfo:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }
    @PostMapping("/selectDropDown")
    public R<List<PmsProductCategory>> selectDropDown(){
        List<PmsProductCategory> list = categoryService.selectDropDown();
        return R.ok(list);
    }

}
